package com.ruoyi.admin.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.admin.domain.Semester;
import com.ruoyi.admin.domain.TLaboratory;
import com.ruoyi.admin.domain.dto.LabRequestDto;
import com.ruoyi.admin.domain.vo.LabRequestVo;
import com.ruoyi.admin.domain.vo.TLaboratoryVo;
import com.ruoyi.admin.service.ISemesterService;
import com.ruoyi.admin.service.ITLaboratoryService;
import com.ruoyi.admin.service.impl.SemesterServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.admin.domain.Labrequest;
import com.ruoyi.admin.service.ILabrequestService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 实验室申请Controller
 * 
 * @author lin
 * @date 2024-04-20
 */
@RestController
@RequestMapping("/lab/labrequest")
public class LabrequestController extends BaseController
{
    @Autowired
    private ILabrequestService labrequestService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ITLaboratoryService itLaboratoryService;

    @Autowired
    private ISemesterService iSemesterService;

    /**
     * 查询实验室申请列表
     */
    @PreAuthorize("@ss.hasPermi('lab:labrequest:list')")
    @GetMapping("/list")
    public TableDataInfo list(LabRequestDto labrequest)
    {
        startPage();
        List<LabRequestVo> list = labrequestService.selectLabrequestList(labrequest);
        return getDataTable(list);
    }

    /**
     * 导出实验室申请列表
     */
    @PreAuthorize("@ss.hasPermi('lab:labrequest:export')")
    @Log(title = "实验室申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LabRequestDto labrequest)
    {
        List<LabRequestVo> list = labrequestService.selectLabrequestList(labrequest);
        ExcelUtil<LabRequestVo> util = new ExcelUtil<LabRequestVo>(LabRequestVo.class);
        util.exportExcel(response, list, "实验室申请数据");
    }

    /**
     * 获取实验室申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('lab:labrequest:query')")
    @GetMapping(value = "/{requestId}")
    public AjaxResult getInfo(@PathVariable("requestId") Long requestId)
    {
        return success(labrequestService.selectLabrequestByRequestId(requestId));
    }

    /**
     * 新增实验室申请
     */
    @PreAuthorize("@ss.hasPermi('lab:labrequest:add')")
    @Log(title = "实验室申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Labrequest labrequest)
    {
        TLaboratoryVo tLaboratoryVo = itLaboratoryService.selectTLaboratoryByLabId(labrequest.getLabId());
        if (tLaboratoryVo==null){
            return error("新增实验室维修信息失败");
        }
        Semester semester = iSemesterService.selectSemesterBySemesterId(labrequest.getSemesterId());
        if (null == semester) {
            return error("新增实验室维修信息失败");
        }
        labrequest.setStudentId(SecurityUtils.getLoginUser().getUser().getUserId());
        return toAjax(labrequestService.insertLabrequest(labrequest));
    }

    /**
     * 修改实验室申请
     */
    @PreAuthorize("@ss.hasPermi('lab:labrequest:edit')")
    @Log(title = "实验室申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Labrequest labrequest)
    {
        SysUser sysUser = sysUserService.selectUserById(labrequest.getStudentId());
        if (sysUser == null){
            return error("修改实验室维修信息失败");
        }
        TLaboratoryVo tLaboratoryVo = itLaboratoryService.selectTLaboratoryByLabId(labrequest.getLabId());
        if (tLaboratoryVo==null){
            return error("修改实验室维修信息失败");
        }
        //目前对学期的限制判断还没做完
//        Semester semester = iSemesterService.selectSemesterBySemesterId(labrequest.getSemesterId());
//        if (null == semester) {
//            return error("修改实验室维修信息失败");
//        }
        return toAjax(labrequestService.updateLabrequest(labrequest));
    }

    /**
     * 删除实验室申请
     */
    @PreAuthorize("@ss.hasPermi('lab:labrequest:remove')")
    @Log(title = "实验室申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{requestIds}")
    public AjaxResult remove(@PathVariable Long[] requestIds)
    {
        return toAjax(labrequestService.deleteLabrequestByRequestIds(requestIds));
    }
}
